package com.omega_r.omegarecyclerview.expandable_example.core;

import com.omega_r.libs.omegarecyclerview.base.BaseProvider;
import com.omega_r.libs.omegarecyclerview.base.BaseViewHolder;
import com.omega_r.omegarecyclerview.ResourceTable;
import com.omega_r.omegarecyclerview.expandable_example.GetData;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.AbsButton;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.RadioButton;

import java.util.ArrayList;
import java.util.List;


public class ExpandableAbility extends Ability implements AbsButton.CheckedStateChangedListener {
    private List<SimpleData> items;
    private BaseProvider<SimpleData> provider;
    private ExpandableViewHolder expandableViewHolder;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_expandable);
        createItems();
        initView();
    }

    private void createItems() {
        items = GetData.getData(this);
    }

    private void initView() {
        ListContainer listContainer = (ListContainer) findComponentById(ResourceTable.Id_recyclerview_core_expand);

        RadioButton radioButtonFade = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_fade);
        RadioButton radioButtonDropdown = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_dropdown);
        radioButtonDropdown.setChecked(true);

        RadioButton radioButtonSingle = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_single);
        radioButtonSingle.setChecked(true);
        RadioButton radioButtonMultiple = (RadioButton) findComponentById(ResourceTable.Id_radiobutton_multiple);
        Button btnTestUpdateChild = (Button) findComponentById(ResourceTable.Id_button_test_update_child);
        radioButtonFade.setCheckedStateChangedListener(this);
        radioButtonDropdown.setCheckedStateChangedListener(this);
        radioButtonSingle.setCheckedStateChangedListener(this);
        radioButtonMultiple.setCheckedStateChangedListener(this);

        provider = new BaseProvider(getContext(), items, ResourceTable.Layout_item_core_adapter);
        provider.setComponentBindingListener(new BaseProvider.ComponentBindingListener() {
            @Override
            public BaseViewHolder creatViewHolder(Component item) {
                expandableViewHolder = new ExpandableViewHolder(item, listContainer);
                return expandableViewHolder;
            }

            @Override
            public void initData(BaseViewHolder baseViewHolder, Object object, int position) {
                ((ExpandableViewHolder) baseViewHolder).updateView((SimpleData) object, radioButtonSingle.isChecked(), radioButtonDropdown.isChecked(), position);
            }
        });
        btnTestUpdateChild.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if (!items.get(3).getmQuotes().get(1).equalsIgnoreCase("UPDATED QUOTE")) {
                    List<SimpleData> simpleData = new ArrayList<>(items);
                    List<String> item = simpleData.get(3).getmQuotes();
                    item.add(1, "UPDATED QUOTE");
                }
                provider.notifyDataSetItemChanged(3);
            }
        });
        listContainer.setItemProvider(provider);
    }

    @Override
    public void onCheckedChanged(AbsButton absButton, boolean b) {
        switch (absButton.getId()) {
            case ResourceTable.Id_radiobutton_fade:
            case ResourceTable.Id_radiobutton_single:
                provider.notifyDataChanged();
                break;
            default:
                break;
        }

    }
}
